﻿using System.Configuration.Install;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Collections;
using log4net;
using System;

namespace Updater.Service
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(ProjectInstaller));

        public ProjectInstaller()
        {
            InitializeComponent();
        }

        public override void Install(IDictionary stateSaver)
        {
            //MessageBox.Show("The install function has been called", "Installer", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);

            log.Debug("Custom Install called with stateSaver value:" + stateSaver);
            base.Install(stateSaver);
            UpdateDataBase(stateSaver);
        }

        /// <summary>
        /// Updates the database according to what is written i the change scripts.
        /// </summary>
        public void UpdateDataBase(IDictionary stateSaver)
        {
            try
            {
                log.Debug("UpdateDataBase() called");
                //Updater.Common.DatabaseInstaller l_DatabaseInstaller = new Updater.Common.DatabaseInstaller();
                //if (!l_DatabaseInstaller.Install())
                //{
                //    this.Rollback(stateSaver);
                //}
            }
            catch (Exception exception)
            {
                this.Rollback(stateSaver);
                log.Error("Error identified during Update of database, RollBack() called with stateSaver value:" + stateSaver, exception);
            }
        }

        private void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
        {

        }

    }
}
